package com.lysj.admin.master.bussiness.admin.service;

import cn.hutool.crypto.digest.BCrypt;
import com.lysj.admin.master.auth.domain.Role;
import com.lysj.admin.master.auth.domain.User;
import com.lysj.admin.master.auth.service.RoleService;
import com.lysj.admin.master.auth.service.UserService;
import com.lysj.admin.master.bussiness.admin.domain.Agent;
import com.lysj.admin.master.bussiness.admin.mapper.AgentMapper;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author Created by drj on 2019-12-27 14:18
 * @description
 */
@Slf4j
@Service
@Transactional
public class AgentService extends MyService<AgentMapper, Agent> {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    public void saveAgent(Agent model,String password) {

        save(model);
        Role role = roleService.lambdaQuery().eq(Role::getType, CommonConstant.AGENT_ISSUER).one();

        //创建对应登录用户信息
        User user = new User();
        user.setUsername(model.getPhone());
        user.setPassword(BCrypt.hashpw(password));
        user.setType(CommonConstant.AGENT_ISSUER);
        user.setRelationId(model.getId());
        user.setRoleId(role.getId());
        userService.save(user);
    }
}
